﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using System.Transactions;
using System.Collections;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using Thrift.Route;

namespace WebServiceSmart.Router
{
    public class MySqlHelperEx
    {
        /*
        private static string LISTBIAONAME = "biaorecordlist";
        private static string HEZUOHUOBANBIAONAME = "biaohezuoproperty";

        private static string DAISHOUBIAONAME = "biaodaishouproperty";
        private static string DELDAISHOUBIAONAME = "biaodeldaishouproperty";
        private static string OVERDAISHOUBIAONAME = "biaooverdaishouproperty";

        private static string GOODSBIAONAME = "biaorecordproperty";
        private static string DELGOODSBIAONAME = "biaodelrecordproperty";
        private static string OVERGOODSBIAONAME = "biaooverrecordproperty";

        private static string CUSTOMBIAONAME = "biaocustom";
        private static string DELCUSTOMBIAONAME = "biaodelcustom";
        private static string SENDCARIDBIAONAME = "biaorecordcarsend";
        private static string NOSENDCARIDBIAONAME = "biaorecordnocarsend";

        private static string WAIZHUANBIAONAME = "biaowaizhuan";
        private static string DAKUANRECORDBIAONAME = "biaodakuanrecord";

        private static string HUIZHANGRECORDBIAONAME = "biaohuizhangrecord";


        private static string EMPLOYEEBIAONAME = "biaoemployee";

        private static string SHOUCHIBIAONAME = "biaoshouchi";
        private static string CARBIAONAME = "biaocar";

        private static string DOWNUSERBIAONAME = "biaodownuser";
        */
        //----------------------------------------------------

        public string DB_Server { get; set; }
        public string DB_Database { get; set; }
        public string DB_UserID { get; set; }
        public string DB_Password { get; set; }
        public string DB_Port { get; set; }
        public string DB_CharacterSet { get; set; }

        #region 构造函数

        public MySqlHelperEx()
        {

        }

        public MySqlHelperEx(string server, string database, string userid, string password, string port, string characterset)
        {
            DB_Server = server;
            DB_Database = database;
            DB_UserID = userid;
            DB_Password = password;
            DB_Port = port;
            DB_CharacterSet = characterset;
        }

        #endregion

        public string GetConnectStr()
        {
            string ret = null;
            MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
            try
            {
                csb.Add("Server", DB_Server);
                csb.Add("Database", DB_Database);
                csb.Add("UserID", DB_UserID);
                csb.Add("Port", DB_Port);
                csb.Add("CharacterSet", DB_CharacterSet);
                ret = csb.GetConnectionString(false);
                ret += String.Format(";password={0};", DB_Password);
            }
            catch
            {
            }
            return ret;
        }

        #region ExecuteNonQuery

        public int ExecuteNonQuery(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteNonQuery(connection, commandText, commandParameters);
        }

        public int ExecuteNonQuery(string connectionString, string commandText, params MySqlParameter[] parms)
        {
            return MySqlHelper.ExecuteNonQuery(connectionString, commandText, parms);
        }

        public int ExecuteNonQuery(string commandText, params MySqlParameter[] commandParameters)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return -1;
            }
            return MySqlHelper.ExecuteNonQuery(connectionString, commandText, commandParameters);
        }

        #endregion

        #region ExecuteDataSet

        public DataRow ExecuteDataRow(string connectionString, string commandText, params MySqlParameter[] parms)
        {
            return MySqlHelper.ExecuteDataRow(connectionString, commandText, parms);
        }

        public DataRow ExecuteDataRow(string commandText, params MySqlParameter[] parms)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return null;
            }
            return MySqlHelper.ExecuteDataRow(connectionString, commandText, parms);
        }

        public DataRow ExecuteDataRow(MySqlConnection connection, string commandText, params MySqlParameter[] parms)
        {
            DataSet ds = MySqlHelper.ExecuteDataset(connection, commandText, parms);
            if (ds == null) return null;
            if (ds.Tables.Count == 0) return null;
            if (ds.Tables[0].Rows.Count == 0) return null;
            return ds.Tables[0].Rows[0];
        }

        public DataRow ExecuteDataRow(MySqlConnection connection, string commandText)
        {
            return ExecuteDataRow(connection, commandText, (MySqlParameter[])null);
        }

        public DataSet ExecuteDataset(string connectionString, string commandText)
        {
            return MySqlHelper.ExecuteDataset(connectionString, commandText);
        }

        public DataSet ExecuteDataset(string connectionString, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteDataset(connectionString, commandText, commandParameters);
        }

        public DataSet ExecuteDataset(MySqlConnection connection, string commandText)
        {
            return MySqlHelper.ExecuteDataset(connection, commandText);
        }

        public DataSet ExecuteDataset(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteDataset(connection, commandText, commandParameters);
        }

        public DataSet ExecuteDataset(string commandText, params MySqlParameter[] commandParameters)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return null;
            }
            return MySqlHelper.ExecuteDataset(connectionString, commandText, commandParameters);
        }

        public void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename)
        {
            MySqlHelper.UpdateDataSet(connectionString, commandText, ds, tablename);
        }

        public void UpdateDataSet(string commandText, DataSet ds, string tablename)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return;
            }
            MySqlHelper.UpdateDataSet(connectionString, commandText, ds, tablename);
        }

        #endregion

        #region ExecuteDataReader

        public MySqlDataReader ExecuteReader(MySqlConnection connection, MySqlTransaction transaction, string commandText, MySqlParameter[] commandParameters, bool ExternalConn)
        {
            //create a command and prepare it for execution
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connection;
            cmd.Transaction = transaction;
            cmd.CommandText = commandText;
            cmd.CommandType = CommandType.Text;

            if (commandParameters != null)
                foreach (MySqlParameter p in commandParameters)
                    cmd.Parameters.Add(p);

            //create a reader
            MySqlDataReader dr;

            // call ExecuteReader with the appropriate CommandBehavior
            if (ExternalConn)
            {
                dr = cmd.ExecuteReader();
            }
            else
            {
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }

            // detach the SqlParameters from the command object, so they can be used again.
            cmd.Parameters.Clear();

            return dr;
        }

        public MySqlDataReader ExecuteReader(string connectionString, string commandText)
        {
            return MySqlHelper.ExecuteReader(connectionString, commandText);
        }

        public MySqlDataReader ExecuteReader(string commandText)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return null;
            }
            return MySqlHelper.ExecuteReader(connectionString, commandText);
        }

        public MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText)
        {
            return MySqlHelper.ExecuteReader(connection, commandText);
        }

        public MySqlDataReader ExecuteReader(string connectionString, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteReader(connectionString, commandText, commandParameters);
        }

        public MySqlDataReader ExecuteReader(string commandText, params MySqlParameter[] commandParameters)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return null;
            }
            return MySqlHelper.ExecuteReader(connectionString, commandText, commandParameters);
        }

        public MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteReader(connection, commandText, commandParameters);
        }

        #endregion

        #region ExecuteScalar

        public object ExecuteScalar(string connectionString, string commandText)
        {
            return MySqlHelper.ExecuteScalar(connectionString, commandText);
        }

        public object ExecuteScalar(string commandText)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return null;
            }
            return MySqlHelper.ExecuteScalar(connectionString, commandText);
        }

        public object ExecuteScalar(string connectionString, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteScalar(connectionString, commandText, commandParameters);
        }

        public object ExecuteScalar(string commandText, params MySqlParameter[] commandParameters)
        {
            string connectionString = GetConnectStr();
            if (connectionString == null)
            {
                return null;
            }
            return MySqlHelper.ExecuteScalar(connectionString, commandText, commandParameters);
        }

        public object ExecuteScalar(MySqlConnection connection, string commandText)
        {
            return MySqlHelper.ExecuteScalar(connection, commandText);
        }

        public object ExecuteScalar(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
        {
            return MySqlHelper.ExecuteScalar(connection, commandText, commandParameters);
        }

        #endregion

    }
}
